package com.dq.service.record.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.TrackRecordQuery;
import com.dq.controller.in.TrackRecordRequest;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.org.Org;
import com.dq.domain.record.TrackRecord;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.org.OrgService;
import com.dq.service.record.TrackRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2020/3/18 10:14
 */
@Service
public class TrackRecordServiceImpl extends BaseRepositories implements TrackRecordService {
    @Autowired
    private OrgService orgService;
    @Override
    public Vo list(TrackRecordQuery trackRecordQuery) throws AdminException {
        trackRecordQuery.format();
        Map<String, Object> query = trackRecordQuery.parseMap();
        if(trackRecordQuery.getOrgId()!=null){
            query.put("sysDept_id",trackRecordQuery.getOrgId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("sysDept" + BaseDaoImpl.inSqlStr, orgList);
        }
        if(StringUtils.isNotEmpty(trackRecordQuery.getValue())){
            Map<String, Object> keyQuery = new HashMap<>();
            keyQuery.put("wxUser_userRealcName"+ BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(trackRecordQuery.getValue()));
            keyQuery.put("wxUser_cardNo"+ BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(trackRecordQuery.getValue()));
            keyQuery.put("wxUser_username" + BaseDaoImpl.likeSqlStr,BaseAdminQuery.likeSql(trackRecordQuery.getValue()));
            query.put("keyQuery" + BaseDaoImpl.orAndSqlStr, keyQuery);
            query.remove("value");
        }
        if(StringUtils.isNotEmpty(trackRecordQuery.getWxUserCardNo())){
            query.put("wxUser_cardNo" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(trackRecordQuery.getWxUserCardNo()));
            query.remove("wxUserCardNo");
        }
        if(StringUtils.isNotEmpty(trackRecordQuery.getWxUserRealcName())){
            query.put("wxUser_userRealcName" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(trackRecordQuery.getWxUserRealcName()));
            query.remove("wxUserRealcName");
        }
        if(trackRecordQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,trackRecordQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(trackRecordQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,trackRecordQuery.getEndTime());
            query.remove("endTime");
        }
        List<TrackRecord> list = dao.find(dao.query2Hql(TrackRecord.class,query),query);
        Long count = dao.count(dao.count2Hql(TrackRecord.class,query),query);
        PageHelper.startPage(trackRecordQuery.getPage(),trackRecordQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<TrackRecord.Vo>) VO.toVOCollection(list));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    @Override
    public Vo save(TrackRecordRequest trackRecordRequest) throws AdminException {
        SysUser sysUser = sysUserService.getUser();
        SysUser wxUser = dao.get(SysUser.class,trackRecordRequest.getWxUserId());
        TrackRecord trackRecord = new TrackRecord();
        trackRecord.setStaffUser(sysUser);
        trackRecord.setSysDept((Org) sysUser.getSection());
        trackRecord.setWxUser(wxUser);
        trackRecord.setTrackContent(trackRecordRequest.getTrackContent());
        trackRecordRepository.save(trackRecord);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo edit(TrackRecordRequest trackRecordRequest,Long id) throws AdminException {
        TrackRecord trackRecord = dao.get(TrackRecord.class,id);
        if(trackRecord == null){
            throw new AdminException("请重新选择");
        }
        trackRecord.setTrackContent(trackRecordRequest.getTrackContent());
        trackRecordRepository.save(trackRecord);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo delete(Long id) throws AdminException {
        TrackRecord trackRecord = dao.get(TrackRecord.class,id);
        if(trackRecord == null){
            throw new AdminException("请重新选择");
        }
        trackRecord.setDeleted(true);
        trackRecordRepository.save(trackRecord);
        return new Vo(HttpStatus.OK);
    }
}
